package com.lewin.ringroad.dao;

import com.lewin.ringroad.entity.DigitalTwinPO;
import com.lewin.ringroad.entity.RadarDirectionPO;
import com.lewin.ringroad.entity.RadarStatisticsDataPO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;

/**
 * 通用 dao
 *
 * @author Jun
 * @since 1.0.0
 */
@Mapper
public interface CommonMapper {

    @Select("""
            select 
            id,
            name,
            biz_type,
            biz_id
            from digital_twin
            where biz_type = 'radarVideoMachine'
            """)
    List<DigitalTwinPO> allRadar();

    @Insert("""
            insert into radar_statistics_data
            (biz_id, traffic, average_speed, occupation_rate, queue_len, time_at)
            values (#{bizId}, #{traffic}, #{averageSpeed}, #{occupationRate}, #{queueLen}, #{timeAt})
            """)
    int insertRadarStatisticsData(RadarStatisticsDataPO po);

    @Select("""
            <script>
            select 
            id, biz_id, traffic, average_speed, occupation_rate,queue_len, time_at
            from radar_statistics_data
            <where>
            <if test="bizId != null">
            biz_id = #{bizId}
            </if>
            and time_at in
            <foreach collection="times" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach> 
            </where>
            </script>
            """)
    List<RadarStatisticsDataPO> listByTimes(@Param("bizId") String bizId, @Param("times") List<String> times);

    @Select("""
            <script>
            select 
            id, biz_id, traffic, average_speed, occupation_rate,queue_len, time_at
            from radar_statistics_data
            <where>
            biz_id in
            <foreach collection="bizIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach> 
            and time_at in
            <foreach collection="times" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach> 
            </where>
            </script>
            """)
    List<RadarStatisticsDataPO> listByBizIdsAndTimes(@Param("bizIds") List<String> bizIds, @Param("times") List<String> times);

    @Insert("""
            <script>
            insert into radar_statistics_data
            (biz_id, traffic, average_speed, occupation_rate, queue_len, time_at)
            values 
            <foreach collection="list" item="item" separator=",">
                (#{item.bizId}, #{item.traffic}, #{item.averageSpeed}, #{item.occupationRate},
                #{item.queueLen}, #{item.timeAt})
            </foreach>
            </script>
            """)
    int batchInsert(Collection<RadarStatisticsDataPO> list);

    @Select("""
            select 
            id, loop_line, direction
            from radar_direction
            """)
    List<RadarDirectionPO> allRadarDirection();

    @Insert("""
            insert into radar_statistics_data
            (biz_id, traffic, average_speed, occupation_rate, queue_len, time_at)
            values
            (#{bizId}, #{traffic}, #{averageSpeed}, #{occupationRate}, #{queueLen}, #{timeAt})
            """)
    int insertTo(RadarStatisticsDataPO po);
}
